<template>
	<view >
		
		<view style="padding-top:30%;">
	    <view class="text-center padding-bottom-lg">
	    	<image src="../../common/img/logo.png" style="width: 200rpx;height: 200rpx;"></image>
	    </view>
		<!-- <view>
			<button type="default" @tap="sendcode">发送验证码</button>
			<button type="default" @tap="toRegister">注册</button>
			<button type="default" @tap="toLogin">登录</button>
			<button type="default" @tap="toMy">我的</button>
		</view> -->
			<view class="operating_box" style="padding-top:10rpx;">
				<view class="cu-form-group ">
					<view class="title">手机号</view>
					<input placeholder="请输入手机号" value="" @input="getsjhm" name="input" maxlength="11"></input>
				</view>
				<view class="cu-form-group solid-bottom">
					<view class="title">验证码</view>
					<input placeholder="请输入验证码" @input="getyzm" name="input"></input>
					<button type="button" @tap="getyzmbtn" :disabled="disabled" v-bind:class="{'default':test}" class='cu-btn bg-green shadow'>{{btnTitle}}</button>
				</view>
				<view class="margin-lg">
					<button class="cu-btn block bg-blue margin-tb-sm lg" @tap="toRegister">
						注册</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sjhm:"",
				yzm:"",
				disabled: false,
				btnTitle:'获取验证码',
				test:true,
				avatar:'',
				nick_name:'',
				openid:'',
				phone:''
			}
		},
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			var code = option.code
			if(code==0)
			{
				/* uni.showToast({
					title:'已是注册用户!',
					icon:'none'
				}) */
				this.openid = option.openid
				uni.setStorageSync("openid",this.openid);		
				//已注册
				uni.navigateTo({
					url: '/pages/user/my/my'
				})
			} else {
				/* uni.showToast({
					title:'授权码是:'+option.openid + '呢称:'+option.nick_nam,
					icon:'none'
				}) */
				this.openid = option.openid
				uni.setStorageSync("openid",this.openid);	
				this.avatar = option.avatar
				this.nick_name = option.nick_name
				
				// uni.navigateTo({
				// 	url: '/pages/user/login/login?logintype=my'
				// })
			}
			
		 //    console.log(option.avatar); //打印出上个页面传递的参数。
		 //    console.log(option.nick_name); //打印出上个页面传递的参数。
			// console.log(option.openid); //打印出上个页面传递的参数。
		},
		onShow() {
			
		},
		methods: {
		
			// getuserinfo(){
			// 	let that=this;
			// 	that.http.postJSON({
			// 		url: 'user/v1/login',
			// 		data: {
			// 			"openid":uni.getStorageSync("openid"),
			// 		}
			// 	}).then((res) => {
			// 		let e=res.data;
			// 		if(e.code==0 && e.success){
			// 			that.info=e.data;
			// 		}
			// 	})
			// },
			getsjhm(e){
				this.sjhm = e.detail.value;
			},
			getyzm(e){
				this.yzm = e.detail.value;
			},
			getyzmbtn(){
				if(this.sjhm.length <= 0){
					uni.showToast({
						title:'请输入手机号!',
						icon:'none'
					})
					return;
				}
				if (!(/^1[3456789]\d{9}$/.test(this.sjhm))){ 
					uni.showToast({
						title:'手机号码格式错误!',
						icon:'none'
					})
					return;
				}
				this.http.postJSON({
					url: 'user/v1/sendcode',
					data:{phone_number:this.sjhm}
				}).then((res) => {
					let e=res.data;
					if(e.code!=0){
						uni.showToast({
							title:e.message,
							icon:'none'
						})
						return;
					}else{
						uni.showToast({
							title:'验证码已发送该手机',
							icon:'none'
						})
						let time = 60;
						let timer = setInterval(()=>{
							this.btnTitle = time + "秒后重试";
							this.disabled = false;
							time--;
							if (time == -1) {
								clearInterval(timer);
								this.btnTitle = "获取验证码";
								this.disabled = false;
							}
						},1000);
					}
				})
			},
			toRegister(){
				if(this.sjhm.length <= 0){
					uni.showToast({
						title:'请输入手机号!',
						icon:'none'
					})
					return;
				}
				if (!(/^1[3456789]\d{9}$/.test(this.sjhm))){ 
					uni.showToast({
						title:'手机号码格式错误!',
						icon:'none'
					})
					return;
				}
				if(this.yzm.length <= 0){
					uni.showToast({
						title:'请输入验证码!',
						icon:'none'
					})
					return;
				}
				let that=this;
				that.http.postJSON({
					url: 'user/v1/reg',
					data: {
						"openid":that.openid,
						"phone_number":that.sjhm,
						"captcha":that.yzm,
						"nick_name":that.nick_name,
						"avatar":that.avatar
					}
				}).then((res) => {
					if(res.data.code ==0 && res.data.success ==true){
						uni.setStorageSync("token",this.openid)
						uni.navigateTo({
							url: '/pages/user/login/login?logintype=my'
						})
						
						// uni.showModal({
						// 	title: '提示',
						// 	content: e.message,
						// 	success: function (res) {
						// 		if (res.confirm) {
						// 			uni.navigateTo({
						// 				url: '/pages/user/my/my'
						// 			})
						// 		} else if (res.cancel) {
						// 			console.log('用户点击取消');
						// 		}
						// 	}
						// });
					}else{
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					}
				})
			},
			toLogin(){
				let that=this;
				that.http.postJSON({
					url: 'user/v1/login',
					data: {
						"openid":"oklcX67Bog5HB6AhCFoGr5yO2R1A",
					}
				}).then((res) => {
						console.log("res="+JSON.stringify(res));
					    uni.setStorageSync("token",res.data.data.token);
					    uni.navigateTo({
					    	url: '/pages/user/my/my'
					    })
				})
			},
			toMy(){
				let that = this
				// let params = {
				// 	// "access_token":"wxc12e65ecba8895fc",
				// }
				// postuserinfo(params).then(res => {
				// 	console.log(res)
					
				// })
				console.log(uni.getStorageSync('TOKEN'))
				uni.request({
				    url: 'http://59.110.116.232:8085/User/v1/getuserinfo', //仅为示例，并非真实接口地址。
					method: 'POST',
				    params: {},
				    header: {
				        'Authorization': "Bearer " + uni.getStorageSync('TOKEN') //自定义请求头信息
				    },
				    success: (res) => {
				        console.log(res);
				        
				    }
				});
			}
		}
	}
</script>
<style>
	page{
		background-color:#f4f4f4;
	}
</style>
